package com.example.lottery_system.common.pojo;

import com.example.lottery_system.common.errcode.ErrorCode;
import com.fasterxml.jackson.databind.ser.std.SerializableSerializer;
import lombok.Data;

/**
 * 实现统一结果返回
 * T 为结果数据的类型
 * msg 错误描述
 * code 错误码
 * @param <T>
 */
@Data
public class CommonResult <T>  {
      private  Integer code;
      private T data;
      private String msg;

//      成功效应
      public static<T> CommonResult<T> success(T data) {
            CommonResult<T> commonResult = new CommonResult<>();
            commonResult.data= data;
            commonResult.code = 200;
            commonResult.msg= "";
            return commonResult;
      }

//      失败响应
      public static<T> CommonResult<T> error(Integer code, String msg) {
            CommonResult<T> commonResult = new CommonResult<>();
            commonResult.code = code;
            commonResult.msg= msg;
            return commonResult;
      }

      /**
       * 再设置一个自定义的错误码类型参数
       *
       * @param errorCode
       * @return
       * @param <T>
       */
      public static<T> CommonResult<T> error(ErrorCode errorCode) {
            CommonResult<T> commonResult = new CommonResult<>();
            commonResult.code = errorCode.getCode();
            commonResult.msg = errorCode.getMsg();
            return commonResult;
      }

}
